package com.CMC.yhdeng;

import java.util.ArrayList;

import com.CMC.yhdeng.QuizContent.QuizExtra;


import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class QuizDisplay extends LinearLayout {
private Context cnt;
	public QuizDisplay(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		cnt=context;
	}

	public QuizDisplay(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		cnt=context;
	}

	public void setContent(QuizContent qc)
	{
		removeAllViews();
		int i=0;
		char[] aryCh=qc.title.toCharArray();
		boolean bRed=(qc.red.size()>0);
		int d1= bRed? qc.red.get(0) : -1, d2=bRed ? qc.red.get(1) : -1;
		for (char c : aryCh) {
			
			int ii=Character.getNumericValue(c);
			
			LinearLayout ll=(LinearLayout )View.inflate(cnt, R.layout.unit_quiz_extra, null);
			
			QuizExtra quizExtra=qc.extraInfo!=null ? qc.extraInfo.get(i) : null; 
			if(quizExtra!=null && quizExtra.upper)
			{
				TextView tvUpper=(TextView )ll.findViewById(R.id.qe_upper);
				tvUpper.setText("`");
			}
			
			
			
			TextView tvHint=(TextView)ll.findViewById(R.id.qe_title);
			String str=String.valueOf(c);
			
			if(quizExtra!=null && quizExtra.lower)
			{
				SpannableString content = new SpannableString(str);
				content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
				tvHint.setText(content);
			}else tvHint.setText(str);
			
			
			if(d1>-1 && i>=d1 && i<d2-1)
			{
				tvHint.setTextColor(Color.RED);
			}else tvHint.setTextColor(Color.BLACK);
			
			
			if(quizExtra!=null)
			{
				String hint=quizExtra.hint;
				if(hint.compareTo("0")!=0)
				{
					TextView tvLower=(TextView )ll.findViewById(R.id.qe_lower);
					tvLower.setText(hint);
				}
			}
			
			this.addView(ll);
			i++;
		}
	}
	public QuizDisplay(Context context, QuizContent qc) {
		super(context);
		// TODO Auto-generated constructor stub
		setOrientation(LinearLayout.HORIZONTAL);
		setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		
		cnt=context;
		setContent(qc);
		 
		
		
		
		
	}

}
